from django.db import models
from accounts.models import User, Technician

class Complaint(models.Model):
    STATUS_CHOICES = (
        ('pending', '待处理'),
        ('processing', '处理中'),
        ('resolved', '已解决'),
        ('rejected', '已驳回'),
    )
    
    TYPE_CHOICES = (
        ('service', '服务质量'),
        ('behavior', '技师行为'),
        ('payment', '支付问题'),
        ('other', '其他问题'),
    )
    
    # 投诉基本信息
    customer = models.ForeignKey(User, on_delete=models.CASCADE, related_name='complaints', verbose_name='投诉用户')
    technician = models.ForeignKey(Technician, on_delete=models.CASCADE, related_name='complaints', verbose_name='被投诉技师')
    order = models.ForeignKey('orders.Order', on_delete=models.CASCADE, related_name='complaints', null=True, blank=True, verbose_name='相关订单')
    
    # 投诉内容
    complaint_type = models.CharField(max_length=20, choices=TYPE_CHOICES, verbose_name='投诉类型')
    title = models.CharField(max_length=100, verbose_name='投诉标题')
    content = models.TextField(verbose_name='投诉内容')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending', verbose_name='处理状态')
    
    # 证据材料
    evidence_images = models.JSONField(default=list, blank=True, verbose_name='证据图片')
    
    # 处理信息
    handler = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, related_name='handled_complaints', verbose_name='处理人')
    resolution = models.TextField(blank=True, verbose_name='处理结果')
    resolved_at = models.DateTimeField(null=True, blank=True, verbose_name='解决时间')
    
    # 时间信息
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        db_table = 'complaint'
        verbose_name = '用户投诉'
        verbose_name_plural = verbose_name
        ordering = ['-created_at']
    
    def __str__(self):
        return f"投诉#{self.id} - {self.title}"